package com.harvest.scientific.ladder.page.fa

import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.harvest.scientific.ladder.R
import com.harvest.scientific.ladder.base.BaseFragment
import com.harvest.scientific.ladder.constant.PageArgs
import com.harvest.scientific.ladder.databinding.FaMineBinding
import com.harvest.scientific.ladder.ext.asLifecycleOwner
import com.harvest.scientific.ladder.ext.isDebug
import com.harvest.scientific.ladder.ext.loadCorner2
import com.harvest.scientific.ladder.ext.notEmpty
import com.harvest.scientific.ladder.ext.visible
import com.harvest.scientific.ladder.page.LoginActivity
import com.harvest.scientific.ladder.page.WebViewActivity
import com.harvest.scientific.ladder.util.LogUtils
import com.harvest.scientific.ladder.util.SpUtil
import com.harvest.scientific.ladder.util.VcUtil
import com.harvest.scientific.ladder.util.isLogin
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

class MineFragment : BaseFragment<FaMineBinding>() {
    private val viewModel by viewModels<MineViewModel>({ requireActivity() })

    private var spokenHeadacheScientificDarknessEveryBottom: String = ""
    companion object {
        fun newInstance() = MineFragment()
    }


    override fun initPage() {
        initListener()
    }

    override fun getTitle(): String {
        return super.getTitle()
    }

    private fun initListener() {

        lifecycleScope.launch {
            launch {
                viewModel.uiStateFlow.collectLatest {
                    when (it) {
                        is MineUiState.GetIdentificationState -> {
                            if (it.spokenHeadacheScientificDarknessEveryBottom == spokenHeadacheScientificDarknessEveryBottom && spokenHeadacheScientificDarknessEveryBottom.notEmpty()) {
                                return@collectLatest
                            }
                            spokenHeadacheScientificDarknessEveryBottom = it.spokenHeadacheScientificDarknessEveryBottom
                            LogUtils.d("GetIdentificationState, afaceUrl:${spokenHeadacheScientificDarknessEveryBottom}")
                            val dp38 = resources.getDimension(R.dimen.dp_38)
                            _binding.spokenIv.loadCorner2(
                                spokenHeadacheScientificDarknessEveryBottom,
                                dp38.toInt(),
                                R.mipmap.ic_launcher,
                                R.mipmap.ic_launcher
                            )
                        }
                        is MineUiState.GetUserInfoState -> {
                            if (it.name1.isNullOrEmpty() && it.name2.isNullOrEmpty()) {
                                _binding.mineNameTv.text = "${getString(R.string.app_name)}"
                            } else {
                                _binding.mineNameTv.text = "${it.name1} ${it.name2}"
                            }
                        }
                    }
                }
            }
        }

        _binding.settingsBtn.setOnClickListener{
            if (VcUtil.isFastDoubleClick(it)){
                return@setOnClickListener
            }
            LanguageSelectActivity.jump(requireActivity())
        }
        _binding.llProtocol.setOnClickListener {
            if (VcUtil.isFastDoubleClick(it)){
                return@setOnClickListener
            }
            WebViewActivity.jumProtocol(requireActivity())
        }
    }

    override fun onResume() {
        super.onResume()
        val isLogin = isLogin()
        _binding.mineNameTv.visible(true)
        if (isLogin) {
            _binding.minePhoneTv.visible(true)
            _binding.minePhoneTv.text = "+221 ${SpUtil.getString(SpUtil.KYE_USER_PHONE, "")}"
            loadData()
        } else {
            _binding.mineNameTv.text = "${getString(R.string.app_name)}"
        }

    }

    override fun loadData() {
        viewModel.sendUiIntent(MineIntent.GetIdentificationIntent)
        viewModel.fleeSocialistExpedition(1)
    }

    override fun isShowActionbar(): Boolean {
        return false
    }
}